package com.megvii.corelib.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.telephony.TelephonyManager;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.UUID;

/**
 * Created by linyue on 16/1/10.
 */
public class AppUtil {
    public static void FakeExitApp(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        //TODO 上边的。。。。。。就先用着System.exit(0)。。。以后再考虑一下用System.exit(0)有什么问题。。。
        System.exit(0);
    }

    public static String getAppVersion(Context context) {
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager
                    .getPackageInfo(context.getApplicationContext().getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "未知版本";
        }
    }

    private static String uuid = null;

    public static String getDeviceUUID(Context context) {
        // 判断本地是否有文件
        if (AppUtil.uuid == null) {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "meg_token";
            File file = new File(path);
            String uuid = null;
            if (file.exists()) {
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                    uuid = reader.readLine();
                    reader.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                uuid = SharedPreferenceTool.getStringValueByKey(context, "device_token", null);
                if (uuid == null) {
                    //没有原始uuid则生成
                    uuid = UUID.randomUUID().toString();
                }
                //写文件
                try {
                    file.createNewFile();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                    writer.write(uuid);
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            AppUtil.uuid = uuid;
        }
        return AppUtil.uuid;
    }

    public static String getIMEI(Context context) {
        if (context != null) {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null && telephonyManager.getDeviceId() != null) {
                return telephonyManager.getDeviceId();
            }
        }
        return null;
    }

    public static String getWIFIMacAddress(Context context) {
        WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
        if (info != null) {
            String macAddress = info.getMacAddress();
            return macAddress;
        }
        return null;
    }

    //    public static void ExitApp(Application application)
    //    {
    //        application.onTerminate();
    //        int currentVersion = android.os.Build.VERSION.SDK_INT;
    //        if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1)
    //        {
    //            Intent startMain = new Intent(Intent.ACTION_MAIN);
    //            startMain.addCategory(Intent.CATEGORY_HOME);
    //            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //            application.startActivity(startMain);
    //            System.exit(0);
    //        }
    //        else
    //        {// android2.1
    //            ActivityManager am = (ActivityManager) application
    //                    .getSystemService(Activity.ACTIVITY_SERVICE);
    //            am.restartPackage(application.getPackageName());
    //        }
    //    }
}
